package com.zhudeshuai.zhudemolist.widget;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.widget.AppCompatSeekBar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.facebook.stetho.common.LogUtil;
import com.zhudeshuai.zhudemolist.R;

import java.io.IOException;


/**
 * 播放器view
 *
 * @author zhudeshuai
 * @since 16/4/25
 */
public class MyPlayer extends RelativeLayout implements SurfaceHolder.Callback, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnPreparedListener {

    private static final String TAG = "MyPlayerView";
    private SurfaceView mSurfaceView;
    private TextView mBack;
    private TextView mPlay;
    private TextView mPause;
    private TextView mStop;
    private SurfaceHolder mHolder;
    private MediaPlayer mediaPlayer;
    private AppCompatSeekBar mSeekBar;

    public MyPlayer(Context context) {
        this(context, null);
        initView(context);
    }

    public MyPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_my_player, this);

        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        mBack = (TextView) findViewById(R.id.tv_back);
        mPlay = (TextView) findViewById(R.id.tv_play);
        mPause = (TextView) findViewById(R.id.tv_pause);
        mStop = (TextView) findViewById(R.id.tv_stop);
        mSeekBar = (AppCompatSeekBar)findViewById(R.id.seekBar);

        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);


        mPlay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                playUrl("/storage/emulated/0/DCIM/Camera/VID_20160425_165332.mp4");
            }
        });
        mPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mStop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDisplay(holder);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnPreparedListener(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        LogUtil.d(TAG, "surfaceChanged...");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        LogUtil.d(TAG, "surfaceDestroyed...");
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        LogUtil.d(TAG, "onBufferingUpdate...");
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        if (mp.getVideoWidth() > 0 && mp.getVideoHeight() > 0) {
            mp.start();
        }
        LogUtil.d(TAG, "onPrepared...");
    }


    /**
     * 播放视频
     *
     * @param url
     */
    public void playUrl(String url) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 暂停
     */
    public void pause() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    /**
     * 停止
     */
    public void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}
